package com.language.points.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * 代理示例 - JDK动态代理抽象类
 * @author yll
 * @version 1.0.0
 * @date 2024/04/25 10：30
 *
 * */
public abstract class JdkProxyHandler implements InvocationHandler {
    Object tarObj;
    Object proxObj = null;
    public JdkProxyHandler(Object tarObj){
        this.tarObj = tarObj;
        this.proxObj = Proxy.newProxyInstance(this.tarObj.getClass().getClassLoader(), this.tarObj.getClass().getInterfaces(), this);
    }
    public Object getProxObj(){
        return this.proxObj;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        invokeAfter(tarObj, proxy, method, args);
        Object resultObj = method.invoke(tarObj, args);
        invokeBefer(tarObj, proxy, method, args);
        return resultObj;
    }

    public abstract void invokeAfter(Object tarObj, Object proxy, Method method, Object[] args);
    public abstract void invokeBefer(Object tarObj, Object proxy, Method method, Object[] args);
}
